<?php
session_start();
if ($_SESSION['user'] == "") {
    ?>
    <script>
        alert('当前未登录！');
        window.location.href = '../login.html';
    </script>
    <?php
}
//连接数据库
try {
    function success($res)
    {
        $result = [
            'error' => 0,
            'message' => $res,
            'time' => time(),
        ];
        return json_encode($result);
    }

    function error($mag)
    {
        $data = [
            'error' => 1,
            'message' => $mag
        ];
        return json_encode($data);
    }

    //连接数据库
    $db = new PDO('mysql:dbname=liaotian;host=106.14.7.147', 'root', '123456');
    $searchTime = $db->prepare('select re.content,re.username,re.time,u.userImg,u.nicheng
                                from roomRecord as re 
                                LEFT JOIN  user as u on u.username=re.username 
                                WHERE  re.time>:time AND  re.roomId=:roomId
                                ORDER BY re.time asc');
    $data = [
        ':roomId' => $_GET['roomId'],
        ':time' => $_GET['lastTime']
    ];
    if ($searchTime->execute($data) === false) {
        print_r($search->errorInfo());
    }
    $result = $searchTime->fetchAll();
    echo success($result);
} catch (PDOException $e) {
    echo 'Connection failed';
}
